package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.Blog;
import com.itheima.pojo.BlogSort;
import com.itheima.pojo.Tag;
import com.itheima.pojo.WebConfig;
import com.itheima.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private LinkService linkService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private BlogSortService blogSortService;

    @Autowired
    private TagService tagService;

    @Autowired
    private FileService fileService;

    @Autowired
    private WebConfigService webConfigService;

    /**
     * 增加友情链接点击数
     * @param uid
     * @return
     */
    @GetMapping("/addLinkCount")
    public Map<String,Object> addLinkCount(@RequestParam("uid") String uid){
        linkService.addLinkCount(uid);
        //封装返回对象
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        result.put("data","更新成功");
        return result;
    }

    /**
     * 通过推荐等级获取博客列表
     * @param level
     * @param currentPage
     * @param useSort
     * @return
     */
    @GetMapping("/getBlogByLevel")
    public Map<String,Object> getBlogByLevel(@RequestParam("level") Integer level,@RequestParam(name = "currentPage",defaultValue = "1",required = false) Long currentPage,@RequestParam("useSort") Integer useSort){
        //获取播客列表
        PageInfo pageInfo = blogService.findByLevelAndSort(level, useSort, currentPage);
        List list = pageInfo.getList();
        List<Map> blogList=new ArrayList<>();
        for (Object o : list) {
            Map map = JSON.parseObject(JSON.toJSONString(o), Map.class);
            //创创建需要封装的集合
            List<String> photoList=new ArrayList<>();
            String fileUid = (String) map.get("fileUid");
            String picUrl = fileService.getPicUrlByUid(fileUid);
            photoList.add(picUrl);

            List<Tag> tagList=new ArrayList<>();
            String tagUid = (String) map.get("tagUid");
            Tag tag = tagService.getByUid(tagUid);
            tagList.add(tag);

            String blogSortUid = (String) map.get("blogSortUid");
            BlogSort blogSort = blogSortService.findBlogByUid(blogSortUid);

            map.put("photoList",photoList);
            map.put("tagList",tagList);
            map.put("blogSort",blogSort);

            //添加集合
            blogList.add(map);
        }
        //封装返回数据
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        //封装data
        Map<String,Object> data=new HashMap<>();
        data.put("records",blogList);
        data.put("total",pageInfo.getTotal());
        data.put("size",pageInfo.getSize());
        data.put("current",pageInfo.getPageNum());
        data.put("orders",new ArrayList<>());
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        //将将data放入返回map
        result.put("data",data);
        return result;
    }

    /**
     * 按时间戳获取博客
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getBlogByTime")
    public Map<String,Object> getBlogByTime(@RequestParam(name = "currentPage",defaultValue = "1",required = false) Long currentPage,@RequestParam(name = "pageSize",defaultValue = "10",required = false) Long pageSize){
        PageInfo pageInfo = blogService.findByTime(Math.toIntExact(currentPage),Math.toIntExact(pageSize));
        //封装返回数据
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        //封装data
        Map<String,Object> data=new HashMap<>();
        data.put("records",pageInfo.getList());
        data.put("total",pageInfo.getTotal());
        data.put("size",pageInfo.getSize());
        data.put("current",pageInfo.getPageNum());
        data.put("orders",new ArrayList<>());
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        //将将data放入返回map
        result.put("data",data);
        return result;
    }

    /**
     * 获取首页排行博客
     * @return
     */
    @GetMapping("/getHotBlog")
    public Map<String,Object> getHotBlog(){
        List<Blog> hotBlog = blogService.findHotBlog();
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        //封装data
        Map<String,Object> data=new HashMap<>();
        data.put("records",hotBlog);
        data.put("total",hotBlog.size());
        data.put("size",5);
        data.put("current",1);
        data.put("orders",new ArrayList<>());
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        //将将data放入返回map
        result.put("data",data);
        return result;
    }

    /**
     * 获取最热标签
     * @return
     */
    @GetMapping("/getHotTag")
    public Map<String,Object> getHotTag(){
        List<Tag> hotTag = tagService.findHotTag();
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        //封装data
        Map<String,Object> data=new HashMap<>();
        data.put("records",hotTag);
        data.put("total",hotTag.size());
        data.put("size",5);
        data.put("current",1);
        data.put("orders",new ArrayList<>());
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        //将将data放入返回map
        result.put("data",data);
        return result;
    }

    /**
     * 获取友情链接
     * @return
     */
    @GetMapping("/getLink")
    public Map<String,Object> getLink(){
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        result.put("data",linkService.findAll());
        return result;
    }

    /**
     * 获取首页最新的博客
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getNewBlog")
    public Map<String,Object> getNewBlog(@RequestParam(name = "currentPage",defaultValue = "1",required = false) Long currentPage,@RequestParam(name = "pageSize",defaultValue = "10",required = false) Long pageSize){
        PageInfo pageInfo = blogService.findNewBlog(Math.toIntExact(currentPage), Math.toIntExact(pageSize));
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        //封装data
        Map<String,Object> data=new HashMap<>();
        data.put("records",pageInfo.getList());
        data.put("total",pageInfo.getPages());
        data.put("size",pageInfo.getPageSize());
        data.put("current",pageInfo.getPageNum());
        data.put("orders",new ArrayList<>());
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        //将将data放入返回map
        result.put("data",data);
        return result;
    }

    /**
     * 获取网站配置
     * @return
     */
    @GetMapping("/getWebConfig")
    public Map<String,Object> getWebConfig(){
        List<WebConfig> webConfigList = webConfigService.getWebConfig();
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        result.put("data",webConfigList);
        return result;
    }
}
